 /**
 * 逻辑、功能相关描述: <br>
 *
 * @author 付湘辉  <br>
 * 编写日期：2008-8-12  <br>
 */
package com.ce.pms.charge.service.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.charge.dao.IBankAccountDao;
import com.ce.pms.charge.dao.ICashieringDao;
import com.ce.pms.charge.model.AbstractSubAccount;
import com.ce.pms.charge.model.AccountDomain;
import com.ce.pms.charge.model.ChargeAccountDomain;
import com.ce.pms.charge.service.IAccountService;
import com.ce.pms.charge.service.ICashieringService;
import com.ce.pms.charge.service.ISubAccountService;
import com.ce.pms.charge.service.ITransactionService;
import com.ce.pms.charge.vo.AbstractTransVO;
import com.ce.pms.charge.vo.AccountVO;
import com.ce.pms.charge.vo.CashieringSearchVO;
import com.ce.pms.charge.vo.CreditTransVO;
import com.ce.pms.charge.vo.DebitTransVO;
import com.ce.pms.charge.vo.HistoryAccountVO;
import com.ce.pms.charge.vo.HouseChargeVO;
import com.ce.pms.charge.vo.AdjustTransactionVO;
import com.ce.pms.charge.vo.PrePaySchChargeVO;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.customer.dao.IClientRHouseDao;
import com.ce.pms.customer.dao.IParticipantRelationDao;
import com.ce.pms.customer.dao.IProRightDao;
import com.ce.pms.customer.model.ProRightDO;
import com.ce.pms.customer.service.IProRightService;
import com.ce.pms.house.dao.ICoalitionHouseDao;
import com.ce.pms.house.model.CoalitionHouseDomain;
import com.ce.pms.house.model.HouseDomain;
import com.ce.pms.house.service.IHouseService;
import com.ce.pms.charge.vo.RelatedHouseChargeVO;
import com.ce.pms.exception.BusinessException;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 	名称: com.ce.pms.charge.service.impl.CashieringServiceImpl <br>
 *   前台收费业务实现
 *
 * @author  付湘辉  <br>
 * Created on： 2008-8-12 <br>
 * @author <br>
 * update on： <br>
 * why: <br>
 */
public class CashieringServiceImpl extends PmsBaseServiceImpl implements
		ICashieringService {
	/**
	 * 帐户
	 */
	private IAccountService accountService;
	/**
	 * 收银dao
	 */
	private ICashieringDao cashieringDao;

	/**
	 * 管理房
	 */
	private ICoalitionHouseDao coalitionHouseDao;

	/**
	 * 房屋
	 */
	private IHouseService houseService;
	/**
	 * 产权Dao
	 */
	private IProRightDao proRightDao;

	/**
	 * 子账户Service
	 */
	private ISubAccountService subAccountService;

	/**
	 * 交易
	 */
	private ITransactionService transactionService;

	/**
	 * 功能描述: 根据房号查询房屋有效的费用VO分页信息 <br>
	 * @param searchVO
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-15  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@SuppressWarnings("unchecked")
	public Page getEffectiveHouseChargeInfo(CashieringSearchVO searchVO){
		super.checkParameterIsNull(searchVO, "common:parameters_null_exception, ");
		searchVO.checkRule();
		super.checkParameterIsNull(searchVO.getHouseNumber(), "common:parameters_null_exception, ");
		searchVO.setLast(false);
		searchVO.setHistory(false);
		searchVO.setDetail(false);
		Page page = this.cashieringDao.getEffectiveHouseChargeInfo(searchVO);
		if (0l == page.getTotalCount()) {
			return page;
		}
		List<HouseChargeVO> data = new ArrayList<HouseChargeVO>();
		List<Object[]> tempList = page.getResult();
		for (Object[] objects : tempList) {
			HouseChargeVO tempvo = this.assemblevObjArray2HouseChargeVo(objects);
			tempvo.setIsHistory(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
			data.add(tempvo);
		}
		page.setResult(data);

		return page;
	}

	/**
	 * 功能描述: 查询指定号码的房间最近一次历史帐户信息 <br>
	 * @param searchVO
	 * @return Page
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-22  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@SuppressWarnings("unchecked")
	public Page getLastHouseChargeInfo(CashieringSearchVO searchVO){
		super.checkParameterIsNull(searchVO, "common:parameters_null_exception, ");
		searchVO.checkRule();
		super.checkParameterIsNull(searchVO.getHouseNumber(), "common:parameters_null_exception, ");
		searchVO.setLast(true);
		searchVO.setHistory(false);
		searchVO.setDetail(false);
		Page page = this.cashieringDao.getLastHouseChargeInfo(searchVO);
		if (0l == page.getTotalCount()) {
			return page;
		}
		List<HouseChargeVO> data = new ArrayList<HouseChargeVO>();
		List<Object[]> tempList = page.getResult();
		for (Object[] objects : tempList) {
			HouseChargeVO tempvo = this.assemblevObjArray2HouseChargeVo(objects);
			tempvo.setIsHistory(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);
			data.add(tempvo);
		}
		page.setResult(data);

		return page;
	}

	/**
	 * 功能描述:　入帐
	 * 　<ul><li>手工记入一笔应收帐款，子帐户是物业帐户，记入交易表借方，作为本金交易 </li></ul>
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-29  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:  principalTran   <br>
	 */
	public DebitTransVO inputAccountForHand(DebitTransVO debitTransVO){
		try {

			super.checkParameterIsNull(debitTransVO, "common:parameters_null_exception, ");
			super.checkParameterIsNull("common:parameters_null_exception, ",debitTransVO.getAccountHouseId());
			//处理子帐户id
			this.dealChargeSubAccountId(debitTransVO);
			
			debitTransVO = this.transactionService.addPrincipalTransaction(debitTransVO);
		} catch (BusinessException be) {
			throw be;
		} catch (Exception e) {
			super.logger.fatal(e.getMessage(),e);
			throw new BusinessException("charge:charge_input_account_exception",BusinessException.FAILURE);
		}
		return debitTransVO;
	}

	/**
	 * 功能描述:　调整
	 * 　<ul>
	 * <li>
	 * 手工记入一笔应收帐款，子帐户是物业帐户，记入交易表借方，
	 * 作为调整交易；在参数adjustTransVO中，必须指定其所属的主交易principalTrans </li>
	 * </ul>
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-29  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:  principalTran   <br>
	 */
	public AdjustTransactionVO inputAdjustAccountForHand(AdjustTransactionVO adjustTransVO){
		try {
			super.checkParameterIsNull(adjustTransVO, "common:parameters_null_exception, ");
			super.checkParameterIsNull("common:parameters_null_exception, ",
										adjustTransVO.getPrincipalTrans(),
										adjustTransVO.getPrincipalTrans().getDebit(),
										adjustTransVO.getHaveReliefPrice(),
										adjustTransVO.getDebit(),
										adjustTransVO.getCurTransPrice());
			adjustTransVO.setTransactionId(null);
			//处理子帐户id
			this.dealChargeSubAccountId(adjustTransVO);
			//判断子账户借方是否锁定
			Boolean isLock = this.subAccountService.isLockDebit(adjustTransVO.getSubAccountId());
			if (isLock) {
				throw new BusinessException("charge:charge_account_debit_locked_exception",
											BusinessException.WARNING);
			}
			//计算调整后金额
			adjustTransVO.calculationAdjustBalance();
			adjustTransVO = this.transactionService.addAdjustTransaction(adjustTransVO);
		} catch (BusinessException be) {
			throw be;
		} catch (Exception e) {
			super.logger.fatal(e.getMessage(),e);
			throw new BusinessException("charge:charge_input_account_exception",BusinessException.FAILURE);
		}
		return adjustTransVO;
	}

	/**
	 * 功能描述:　减免
	 * 　<ul>
	 * <li>
	 * 手工记入一笔应收帐款，子帐户是物业帐户，
	 * 记入交易表借方，作为减免交易；在参数adjustTransVO中，
	 * 必须指定其所属的主交易principalTrans
	 * </li>
	 * </ul>
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-29  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:  principalTran   <br>
	 */
	public AdjustTransactionVO inputReliefAccountForHand(AdjustTransactionVO reliefTransVO){
		try {
			super.checkParameterIsNull(reliefTransVO, "common:parameters_null_exception, ");
			super.checkParameterIsNull("common:parameters_null_exception, ",
										reliefTransVO.getPrincipalTrans(),
										reliefTransVO.getPrincipalTrans().getDebit(),
										reliefTransVO.getHaveReliefPrice(),
										reliefTransVO.getDebit(),
										reliefTransVO.getCurTransPrice());
			reliefTransVO.setTransactionId(null);
			//处理子帐户id
			this.dealChargeSubAccountId(reliefTransVO);
			//判断子账户借方是否锁定
			Boolean isLock = this.subAccountService.isLockDebit(reliefTransVO.getSubAccountId());
			if (isLock) {
				throw new BusinessException("charge:charge_account_debit_locked_exception",
											BusinessException.WARNING);
			}
			//计算减免后金额
			reliefTransVO.calculationReliefBalance();

			reliefTransVO = this.transactionService.addReliefTransaction(reliefTransVO);
		} catch (BusinessException be) {
			throw be;
		} catch (Exception e) {
			super.logger.fatal(e.getMessage(),e);
			throw new BusinessException("charge:charge_input_account_exception",BusinessException.FAILURE);
		}
		return reliefTransVO;
	}

	/**
	 * 功能描述: 手工缴纳费用，记入交易贷方，作为实收交易 <br>
	 * @param creditTransVO
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-29  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public CreditTransVO payCostsForHand(CreditTransVO creditTransVO){

		try {
			super.checkParameterIsNull("common:parameters_null_exception, ",
					creditTransVO,creditTransVO.getAccountHouseId());
			this.dealAccountId(creditTransVO);
			this.dealChargeSubAccountId(creditTransVO);
			creditTransVO = this.transactionService.addChargeTransaction(creditTransVO);
		} catch (BusinessException be) {
			throw be;
		} catch (Exception e) {
			super.logger.fatal(e.getMessage(), e);
			throw new BusinessException("charge:charge_pay_cost_failure_exception",
						BusinessException.FAILURE);
		}
		return creditTransVO;
	}

	/**
	 * 功能描述: 查询指定房屋产权帐户的详细信息  <br>
	 * @param searchVo
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-27  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public HistoryAccountVO queryHouseChargeDetailInfo(CashieringSearchVO searchVo){
		super.checkParameterIsNull(searchVo, "common:parameters_null_exception, ");
		super.checkParameterIsNull("common:parameters_null_exception,ID	",searchVo.getHouseId(),
																		  searchVo.getProRightId(),
																		  searchVo.getAccountId());
		searchVo.setDetail(true);
		searchVo.setLast(false);
		searchVo.setHistory(false);
		Object[] objArray = this.cashieringDao.queryHouseChargeDetailInfo(searchVo);
		if (BlankUtil.isBlank(objArray)) {
			return null;
		}
		HistoryAccountVO detailVo = this.assemblevObjArray2HistoryAccountVo(objArray);
		String isHis = objArray[14]==null?null:((Character)objArray[14]).toString();
		if (SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(isHis)) {
			isHis=SysrefcodeDO.CONSTANTS_DIGITAL_ZERO;
		}else{
			isHis=SysrefcodeDO.CONSTANTS_DIGITAL_ONE;
		}
		detailVo.setIsHistory(isHis);

		//	处理划款银行帐号信息
		this.getBankAccountInfo(detailVo, objArray[15]==null?null:(String)objArray[15]);
		// 处理相关房间信息
		Set<RelatedHouseChargeVO> relHouses = this.dealRelatedHouseInfo(detailVo);

		detailVo.setRelHouses(relHouses);
		return detailVo;
	}

	/**
	 * 功能描述: 查询房间历史帐户分页信息 <br>
	 * @param searchVo
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Page queryHouseChargeHistoryListByPage(CashieringSearchVO searchVo){
		super.checkParameterIsNull(searchVo, "common:parameters_null_exception, ");
		searchVo.checkRule();
		super.checkParameterIsNull(searchVo.getHouseId(), "common:parameters_null_exception,ID");
		searchVo.setHistory(true);
		searchVo.setLast(false);
		searchVo.setDetail(false);
		Page page = this.cashieringDao.queryHouseChargeHistoryListByPage(searchVo);

		List<HistoryAccountVO> hcList = new ArrayList<HistoryAccountVO>();
		if (!BlankUtil.isBlank(page.getResult())) {
			List list = page.getResult();
			for (Object object : list) {
				Object[] objs = (Object[])object;
				HistoryAccountVO hcVo = this.assemblevObjArray2HistoryAccountVo(objs);
				hcVo.setIsHistory(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);
				hcList.add(hcVo);
			}
			page.setResult(hcList);
		}

		return page;
	}

	/**
	 * 功能描述: 根据总账户id查询收费子账户的滞纳金金额，
	 * 只查询未缴状态的，并且计算调整和减免。 <br>
	 * @param accountId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-11-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public BigDecimal queryLatefeeByAccountId(Long accountId){
		super.checkParameterIsNull(accountId, "common:parameters_null_exception,ID");
		return this.cashieringDao.queryLatefeeByAccountId(accountId);
	}

	/**
	 * 功能描述: 收取一笔预交款 <br>
	 * @param prePaySchChargeVO 预交款账户收款VO
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-9-8  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public PrePaySchChargeVO receivedPrePaySchCharge(PrePaySchChargeVO prePaySchChargeVO){
		try {
			super.checkParameterIsNull("common:parameters_null_exception, ",
					prePaySchChargeVO);
			super.checkParameterIsNull("common:parameters_null_exception, ",
					prePaySchChargeVO.getAccountHouseId());

			prePaySchChargeVO = this.transactionService
					.addPrePayChargeTransaction(prePaySchChargeVO);
		} catch (BusinessException be) {
			throw be;
		} catch (Exception e) {
			super.logger.fatal(e.getMessage(), e);
			throw new BusinessException("charge:charge_received_pre_charge_failure_exception",
						BusinessException.FAILURE);
		}
		return prePaySchChargeVO;
	}

	/**
	 * @param accountService the accountService to set
	 */
	public void setAccountService(IAccountService accountService) {
		this.accountService = accountService;
	}

	/**
	 * @param bankAccountDao the bankAccountDao to set
	 */
	public void setBankAccountDao(IBankAccountDao bankAccountDao) {
	}

	/**
	 * @param cashieringDao the cashieringDao to set
	 */
	public void setCashieringDao(ICashieringDao cashieringDao) {
		this.cashieringDao = cashieringDao;
	}

	/**
	 * @param clientRHouseDao the clientRHouseDao to set
	 */
	public void setClientRHouseDao(IClientRHouseDao clientRHouseDao) {
	}


	/**
	 * @param coalitionHouseDao the coalitionHouseDao to set
	 */
	public void setCoalitionHouseDao(ICoalitionHouseDao coalitionHouseDao) {
		this.coalitionHouseDao = coalitionHouseDao;
	}

	/**
	 * @param houseService the houseService to set
	 */
	public void setHouseService(IHouseService houseService) {
		this.houseService = houseService;
	}

	/**
	 * @param participantRelationDao the participantRelationDao to set
	 */
	public void setParticipantRelationDao(
			IParticipantRelationDao participantRelationDao) {
	}

	/**
	 * @param proRightDao the proRightDao to set
	 */
	public void setProRightDao(IProRightDao proRightDao) {
		this.proRightDao = proRightDao;
	}

	/**
	 * @param proRightService the proRightService to set
	 */
	public void setProRightService(IProRightService proRightService) {
	}

	public void setSubAccountService(ISubAccountService subAccountService) {
		this.subAccountService = subAccountService;
	}

	/**
	 * @param transactionService the transactionService to set
	 */
	public void setTransactionService(ITransactionService transactionService) {
		this.transactionService = transactionService;
	}

	/** 功能描述:  <br>
	 * @param objArray
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private HistoryAccountVO assemblevObjArray2HistoryAccountVo(Object[] objArray) {
		HouseChargeVO hcvo = this.assemblevObjArray2HouseChargeVo(objArray);
		HistoryAccountVO hisaVo = new HistoryAccountVO(hcvo);
		hisaVo.setChangeProRightDate(objArray[13]==null?null:(Date)objArray[13]);//产权变更日期
		return hisaVo;
	}

	/** 功能描述:  <br>
	 * @param objArray
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private HouseChargeVO assemblevObjArray2HouseChargeVo(Object[] objArray) {
		HouseChargeVO hcVo = new HouseChargeVO();
		hcVo.setHouseId(objArray[0]==null?null:((BigInteger)objArray[0]).longValue());//房屋id
		hcVo.setHouseNumber(objArray[1]==null?null:(String)objArray[1]);//房屋号码
		hcVo.setIsVirtual(objArray[2]==null?null:((Character)objArray[2]).toString());//是否是管理房
		hcVo.setParticipantName(objArray[3]==null?null:(String)objArray[3]);//业主姓名
		hcVo.setTelephone(objArray[4]==null?null:(String)objArray[4]);//业主联系电话
		hcVo.setChargeSelect(objArray[5]==null?null:((Character)objArray[5]).toString());//记帐策略
		hcVo.setBalance(objArray[6]==null?BigDecimal.ZERO:(BigDecimal)objArray[6]);//收费账户余额
		hcVo.setPrePayBalance(objArray[7]==null?BigDecimal.ZERO:(BigDecimal)objArray[7]);//预交款帐户余额
		hcVo.setDeposit(objArray[8]==null?BigDecimal.ZERO:(BigDecimal)objArray[8]);//保证金/押金帐户余额
		hcVo.setLatefee(objArray[9]==null?BigDecimal.ZERO:(BigDecimal)objArray[9]);//滞纳金
		hcVo.setIsChangeProRight(objArray[10]==null?null:(String)objArray[10]);//产权变更标志
		hcVo.setProRightId(objArray[11]==null?null:((BigInteger)objArray[11]).longValue());//产权id
		hcVo.setAccountId(objArray[12]==null?null:((BigInteger)objArray[12]).longValue());//帐户id
		return hcVo;
	}

	/**
	 * 功能描述: 如果总账id为空，则进行查询得到 <br>
	 * @param abstractTransVO 交易VO，其中房屋id必须有
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-9-8  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private void dealAccountId(AbstractTransVO abstractTransVO){
		if (BlankUtil.isBlank(abstractTransVO.getAccountId())) {
			super.checkParameterIsNull("common:parameters_null_exception, ",
					abstractTransVO.getAccountHouseId());
			ProRightDO proRight = this.proRightDao.getEffectiveProRightByHouseId(abstractTransVO.getAccountHouseId());
			if (BlankUtil.isBlank(proRight)) {
				return;
			}
			AccountDomain account =this.accountService.currentAccount(abstractTransVO.getAccountHouseId(), proRight);
			if (BlankUtil.isBlank(account)) {
				return;
			}
			abstractTransVO.setAccountId(account.getAccountId());
		}
	}

	/** 功能描述: 处理物业收费子帐户id，要求已有总账id <br>
	 * @param abstractTransVO
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-9-5  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	private void dealChargeSubAccountId(AbstractTransVO abstractTransVO) {
		if (BlankUtil.isBlank(abstractTransVO.getSubAccountId())) {
			super.checkParameterIsNull("common:parameters_null_exception, ",
					abstractTransVO.getAccountHouseId());
			abstractTransVO.setSubAccountId(this.getChargeSubAccountIdByHouseId(abstractTransVO.getAccountHouseId()));
			if (BlankUtil.isBlank(abstractTransVO.getSubAccountId())) {
				throw new BusinessException("charge:charge_not_subaccount_id_exception",BusinessException.WARNING);
			}
		}
	}

	/** 功能描述: 处理相关房间信息,目前只得当前相关的房间信息 <br>
	 * @param detailVo
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-28  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	private Set<RelatedHouseChargeVO> dealRelatedHouseInfo(HistoryAccountVO detailVo) {
		CoalitionHouseDomain chouse = null;
		if (HouseChargeVO.IS_HISTORY_1.equals(detailVo.getIsHistory())) {
			return null;
		}

		Set<RelatedHouseChargeVO> relHouses = new HashSet<RelatedHouseChargeVO>();

		if (SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(detailVo.getIsVirtual())) {//是管理房
			chouse = this.coalitionHouseDao.findById(detailVo.getHouseId());
		}else{//是产权房
			chouse = this.houseService.getCoalition(detailVo.getHouseId());//得到管理房
			if (!BlankUtil.isBlank(chouse)) {
				ProRightDO pro = null;
				if (SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(detailVo.getChargeSelect())){//分开记账
					pro = this.proRightDao.getLastProRightByHouseId(chouse.getKeyword(),true);
				}else{
					pro = this.proRightDao.getEffectiveProRightByHouseId(chouse.getKeyword());
				}

				if (!BlankUtil.isBlank(pro)) {
					AccountVO accv = this.accountService.getAccountByProRightAndHouse(pro.getProRightId(), chouse.getKeyword(),false);
					relHouses.add(new RelatedHouseChargeVO(chouse.getKeyword(),chouse.getHouseNumber(),pro.getProRightId(),accv==null?null:accv.getAccountId()));
				}else{
					relHouses.add(new RelatedHouseChargeVO(chouse.getKeyword(),chouse.getHouseNumber(),null,null));
				}
			}
		}

		if (!BlankUtil.isBlank(chouse)) {
			Set<HouseDomain> set = chouse.getHouses();
			for (HouseDomain domain : set) {
				if (domain.getKeyword().equals(detailVo.getHouseId())) {
					continue;
				}
				ProRightDO pro = null;
				AccountVO accv = null;
				//如果是分开记账
				if (SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(detailVo.getChargeSelect())) {
					pro = this.proRightDao.getEffectiveProRightByHouse(domain);
					accv = this.accountService.getCurrentAccountBy(pro.getProRightId(), domain.getKeyword());
				}else{
					//如果是合并记账
					pro = this.proRightDao.getLastProRightByHouseId(domain.getKeyword(),false);
					if (BlankUtil.isBlank(pro)) {
						relHouses.add(new RelatedHouseChargeVO(domain.getKeyword(),domain.getHouseNumber(),null,null));
						continue;
					}
					accv = this.accountService.getAccountByProRightAndHouse(pro.getProRightId(), domain.getKeyword(),false);
				}

				relHouses.add(new RelatedHouseChargeVO(domain.getKeyword(),domain.getHouseNumber(),pro==null?null:pro.getProRightId(),accv==null?null:accv.getAccountId()));
			}
		}
		return relHouses;
	}

	/** 功能描述: 处理划款银行帐号信息 <br>
	 * @param tempvo
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	private void getBankAccountInfo(HistoryAccountVO historyVo,String bankAccountInfoStr) {
		try {
			String[] bankAccounts = bankAccountInfoStr.split(SysrefcodeDO.CONSTANTS_COMMA,3);
			if (!BlankUtil.isBlank(bankAccounts)) {
				historyVo.setBankAccountId(Long.valueOf(bankAccounts[0]));
				historyVo.setAccNumber(bankAccounts[1]);
				historyVo.setAccOwner(bankAccounts[2]);
			}
		} catch (Exception e) {

		}
	}

	/** 功能描述: 根总账得到其下的子账户，
	 * 如果isPrePaySch为true,则只查预交款账户 <br>
	 * @param account 总账户
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-9-5  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	private Long getChargeAccountSubAccountId(AccountDomain account) {
		Long resId = null;
		Set<AbstractSubAccount> set = account.getSubAccounts();
		for (AbstractSubAccount subAccount : set) {
			if (subAccount instanceof ChargeAccountDomain) {
				resId = subAccount.getSubAccountId();
				break;
			}
		}
		return resId;
	}

	/** 功能描述: 得到指定房间有效的帐户下子帐户id <br>
	 * @param accountHouseId 账户所属房屋id
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-9-4  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	private Long getChargeSubAccountIdByHouseId(Long accountHouseId) {
		ProRightDO proRight = this.proRightDao.getEffectiveProRightByHouseId(accountHouseId);
		if (BlankUtil.isBlank(proRight)) {
			return null;
		}

		AccountDomain account =this.accountService.currentAccount(accountHouseId, proRight);
		if (BlankUtil.isBlank(account)) {
			return null;
		}
		Long subAccountId = this.getChargeAccountSubAccountId(account);

		return subAccountId;
	}
}

